﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using TTSIDAL;
using TTSModel;

namespace TTSDAL
{
    public class Assemble : BaseRepository_DT<AssembleInfo>, IAssemble
    {
        #region 静态变量定义
        private const string PARM_ASSEMBLE_ID = "@ID";
        private const string PARM_ASSEMBLE_SN = "@SN";
        private const string PARM_ASSEMBLE_TCODE = "@Tcode";
        private const string PARM_ASSEMBLE_RECORDTIME = "@RecordTime";
        private const string PARM_ASSEMBLE_USERID = "@UserID";
        private const string PARM_ASSEMBLE_ISROOT = "@IsRoot";

        private const string PARM_ASSEMBLE_DT = "@AssemblyItemdt";
        #endregion

        #region 数据检索部分
        public IList<AssembleInfo> LoadEntities()
        {
            return null;
        }

        public AssembleInfo LoadEntity<T1>(T1 id)
        {
            SqlParameter parm = GetParameter();
            parm.Value = id;
            return ExcuteSqlToGetEntity("Assembly_selectbyID", parm);
        }

        public bool Assemble_Insert(AssembleInfo ai, DataTable dt)
        {
            List<SqlParameter> ItemList = new List<SqlParameter>() {
            sqlHelper.CreateSqlParemeterStructured(PARM_ASSEMBLE_DT,dt)};
            SqlParameter[] Parms = GetParameters();
            SetParameters(ai, Parms);

            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            foreach (SqlParameter parm in ItemList)
                cmd.Parameters.Add(parm);

            cmd.Parameters.AddWithValue("@return", "").Direction = ParameterDirection.ReturnValue;

            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "Assembly_Insert";
                cmd.ExecuteNonQuery();
                bool isOK = cmd.Parameters["@return"].Value.ToString() == "1" ? true : false;
                return isOK;
            }
        }

        public AssembleInfo Assemble_GetbySN(string sn)
        {
            SqlParameter parm = new SqlParameter(PARM_ASSEMBLE_SN, SqlDbType.NVarChar, 50);
            parm.Value = sn;
            return ExcuteSqlToGetEntity("Assembly_selectbySN", parm);
        }
        public void Assemble_DeletebySN(string sn, string storedProcedureName)
        {
            SqlParameter parm = new SqlParameter(PARM_ASSEMBLE_SN, SqlDbType.NVarChar, 50);
            parm.Value = sn;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(parm);
            try
            {
                ExecuteCommand(cmd, storedProcedureName);
            }
            catch
            {
                throw;
            }
        }

        public IList<AssembleInfo> Assemble_GetbyDT(DateTime dt1, DateTime dt2)
        {
            SqlParameter[] parms = GetDTParameters();
            parms[0].Value = dt1;
            parms[1].Value = dt2;
            return ExcuteSqlToGetEntities("Assembly_selectbyDT", parms);
        }
        #endregion

        #region 重写基类方法
        public override AssembleInfo GetEntityRecord(SqlDataReader rdr)
        {
            AssembleInfo entity = new AssembleInfo()
            {
                ID = rdr.GetInt32(0),
                SN = rdr.GetValue(1).ToString(),
                Tcode = rdr.GetValue(2).ToString(),
                RecordTime = rdr.GetDateTime(3),
                Userid = rdr.GetValue(4).ToString(),
                IsRoot = rdr.GetBoolean(5)
            };
            return entity;
        }

        public override AssembleInfo GetEntityRecord()
        {
            AssembleInfo entity = new AssembleInfo();
            return entity;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter parm = new SqlParameter(PARM_ASSEMBLE_ID, SqlDbType.Int, 4);
            return parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = new SqlParameter[]
            {
                new SqlParameter(PARM_ASSEMBLE_ID,SqlDbType.Int,4),
                new SqlParameter(PARM_ASSEMBLE_SN,SqlDbType.NVarChar,50),
                new SqlParameter(PARM_ASSEMBLE_TCODE,SqlDbType.NChar,8),
                new SqlParameter(PARM_ASSEMBLE_RECORDTIME,SqlDbType.DateTime,8),
                new SqlParameter(PARM_ASSEMBLE_USERID,SqlDbType.NVarChar,6),
                new SqlParameter(PARM_ASSEMBLE_ISROOT,SqlDbType.Bit,1),
            };
            return parms;
        }

        public override void SetParameters(AssembleInfo Entity, params SqlParameter[] Parms)
        {
            Parms[0].Value = Entity.ID;
            Parms[1].Value = Entity.SN;
            Parms[2].Value = Entity.Tcode;
            Parms[3].Value = Entity.RecordTime;
            Parms[4].Value = Entity.Userid;
            Parms[5].Value = Entity.IsRoot;
        }
        #endregion
    }
}
